package com.binguo.helper;

import com.alibaba.fastjson.JSONObject;
import com.binguo.common.exception.ServiceException;
import com.binguo.model.wxapi.dto.SessionDTO;
import com.binguo.model.wxapi.dto.UserInfoDTO;
import com.binguo.wxapp.AesCbcUtil;
import com.binguo.wxapp.HttpRequest;
import org.springframework.stereotype.Component;

import static com.binguo.common.exception.ErrorCode.WX_DECRYPT_ERROR;

/**
 * Created by haibang on 18/5/19.
 */
@Component
public class WxAppHelper {

    public static String APPID = "wx099356561cb9c60f";
    public static String SECRET= "18e82647d37653d37803010e2eaca6a8";
    public static String ORDERINGAPPID = "wx34cdf891381ba921";
    public static String ORDERINGSECRET = "4914408c0bda9a1591051c0416f8120d";
    String grant_type = "authorization_code";


    public SessionDTO getSession(String code,String appid,String secret){
        String params = "appid=" + appid + "&secret=" + secret + "&js_code=" + code + "&grant_type=" + grant_type;
        String sr = HttpRequest.sendGet("https://api.weixin.qq.com/sns/jscode2session", params);
        System.out.println(sr);
        JSONObject json = JSONObject.parseObject(sr);
        String sessionkey = json.get("session_key").toString();
        String openId = (String) json.get("openid");
        return SessionDTO.builder()
                .sessionKey(sessionkey)
                .openId(openId).build();
    }

    public UserInfoDTO decryptUserInfo(String sessionKey, String encryptedData, String iv){
        try {
            String result = AesCbcUtil.decrypt(encryptedData, sessionKey, iv, "UTF-8");
            if (null != result && result.length() > 0) {
                JSONObject json = JSONObject.parseObject(result);
                String openId = (String) json.get("openId");
                String nickName = (String) json.get("nickName");
                Integer gender = (Integer) json.get("gender");
                String city = (String) json.get("city");
                String province = (String) json.get("province");
                String country = (String) json.get("country");
                String avatarUrl = (String) json.get("avatarUrl");
                String unionId = (String) json.get("unionId");

                return UserInfoDTO.builder()
                        .openId(openId)
                        .nickName(nickName)
                        .gender(gender)
                        .city(city)
                        .province(province)
                        .country(country)
                        .avatarUrl(avatarUrl)
                        .unionId(unionId).build();
            } else throw new ServiceException(WX_DECRYPT_ERROR);

        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(WX_DECRYPT_ERROR);
        }

    }

    public String decryptPhone(String sessionKey, String encryptedData, String iv){
        try {
            String result = AesCbcUtil.decrypt(encryptedData, sessionKey, iv, "UTF-8");
            if (null != result && result.length() > 0) {
                JSONObject json = JSONObject.parseObject(result);
                return (String) json.get("purePhoneNumber");
            } else throw new ServiceException(WX_DECRYPT_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(WX_DECRYPT_ERROR);
        }

    }
//
//    public UserInfoDTO  decryptUserInfo1() {
//
//        String key = "ae4cee15472ca695ce4941d9c0599f25";
//
//        String key1 = "LP2FOdgFqJ9SlMA9mF+EGA==";
//
//        String encryptedData = "qfSYjmSNPGSwjwysvoskwLQoMq7IK/2coEVDHtT0yumNNRVXfvQ92ZaKD552m2WEyPEOo1bfOky5koG9S0oBgvQbENpMSFgGStFbuuqQZQKF3RHUl4tPdfFpJ/od0n90xMc5EkKcCsdM4460aXe8oGl8OZkx1f2mVlvh92p9P6DHP9V7TqfZVFZbXOLKoqzwsFsKcZWZymWx5fLpkTi9WvQwvE3PRGFxeoWQvB96TSqSadMbLmou7/qPg8H9k6tAJlTsc7tFE/WJmru5w/bjAinMiFsO15LM+obu0qq3kUPQYMNlqGg1X6ZJOfRx+uFGrzJYbxPXSEl3bg26xLeBOyCfLir9QA78XZdBwhCkLVW+OCRv0+DsfDpb+uqfPu3+rnxl+oZ6nJufb89xKFw6kYu+wpEH7vfSEBeW44vIe5Zn4DJNZpGreDkpZiBHq4kjdtAYEwrnV1ko/TVfGnZFiQPipd3hrGfTLjalJCxxJfRoMrPIXDtM4++TWOXBrFNBx02T+l6Ffq2LN3e1s+yKGw==";
//        String vi = "f/DAyXm8qAiRYjztKeEOtQ==";
//
//        return decryptUserInfo(key1, encryptedData, vi);
//    }

}
